﻿namespace MSSqlSchemaDoc.Provider.MicrosoftSqlServer
{
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;
    using MSSqlSchemaDoc.Core.DataStructures;
    using MSSqlSchemaDoc.Provider.MicrosoftSqlServer.Helpers;
    using MSSqlSchemaDoc.Provider.MicrosoftSqlServer.Resources;

    /// <summary>
    /// Contains the helper methods for full text catalogs.
    /// </summary>
    public partial class SqlScriptingManager
    {
        /// <summary>
        /// Fetches a list of full text catalogs and extracts the information for each.
        /// </summary>
        /// <param name="connection">An open connection to the database.</param>
        /// <param name="objectType">The full text catalog object type.</param>
        private void ScriptFullTextCatalogs(SqlConnection connection, DataStructureObjectType objectType)
        {
            // Load data
            StringBuilder query = new StringBuilder(this.queryManager.FullTextCatalogs());
            SqlCommand c = new SqlCommand(query.ToString(), connection);

            using (IDataReader readerFt = c.ExecuteReader())
            {
                // Build the full text catalog objects
                while (readerFt.Read())
                {
                    string name = DataReaderHelper.GetString(readerFt, "ObjectName");

                    this.OnBeginScriptingObject(name);

                    FullTextCatalog ft = new FullTextCatalog();

                    ft.ObjectId = DataReaderHelper.GetInt(readerFt, "ObjectId").Value;
                    ft.Name = name;
                    ft.IsDefault = DataReaderHelper.GetBool(readerFt, "IsDefault");
                    ft.IsAccentSensitivityOn = DataReaderHelper.GetBool(readerFt, "IsAccentSensitivityOn");

                    // Documentation
                    if (this.IncludeDocumentationObjects)
                    {
                        ft.Documentation = this.ConstructDocumentationObject<FullTextCatalogDocumentation>(ft.Name, string.Empty);
                    }

                    this.OnCompleteScriptingObject(ft, name);
                }
            }
        }
    }
}
